A sequence of n real numbers is given. Find the sum of all its
elements.
Input. The first line contains one integer n (n
≤ 100). The second line contains n
real numbers, each with an absolute value not exceeding 100.
Output. Print the sum of all elements in the sequence,
rounded to one decimal place.
Samle input |
Sample output |
5 1.2 1.3 5.7 1.8 12.4 |
22.4 |
loops
Compute the sum of the numbers using a loop.
Declare an array.
double m[100];
Read the input data.
scanf("%d",&n);
for
(i = 0; i < n; i++)
scanf("%lf",&m[i]);
In the variable s, compute the sum of all numbers in the array m.
s = 0;
for(i = 0; i < n; i++)
s = s + m[i];
Print the resulting sum.
printf("%.1lf\n",s);
Read the input value of n.
scanf("%d",&n);
In the variable s, compute the sum of n numbers.
s = 0;
for (i = 0; i < n; i++)
{
scanf("%lf",&x);
s = s + x;
}
Print the answer.
printf("%.1lf\n",s);
Algorithm implementation – vector
Declare a vector m to store the input array.
vector<double> m;
Read the input data.
scanf("%d", &n);
m.resize(n);
for (i = 0; i < n; i++)
scanf("%lf", &m[i]);
In the variable s, compute the sum of all numbers in the array m.
s = 0;
for (i = 0; i < n; i++)
s = s + m[i];
Print the answer.
printf("%.1lf\n", s);
Algorithm implementation – accumulate
Declare a vector v to store the input array.
vector<double> v;
Read the input data.
scanf("%d", &n);
v.resize(n);
for (i = 0; i < n; i++)
scanf("%lf", &v[i]);
Compute the sum
of the numbers in the array v using the accumulate function.
s = accumulate(v.begin(),
v.end(), 0.0);
Print the answer.
printf("%.1lf\n", s);
Algorithm implementation – dynamic memory allocation, malloc
#include <stdio.h>
#include <malloc.h>
int i, n;
double s, *m;
int main(void)
{
scanf("%d", &n);
m = (double *)malloc(n * sizeof(double));
for (i = 0; i <
n; i++) // read array
scanf("%lf", &m[i]);
s = 0; // find the sum
of array elements
for (i = 0; i <
n; i++)
s = s + m[i];
free(m);
printf("%.1lf\n", s); // print the
answer
return 0;
}
Algorithm implementation – dynamic memory allocation, calloc
#include <stdio.h>
#include <malloc.h>
int i, n;
double s, *m;
int main(void)
{
scanf("%d", &n);
m = (double *)calloc(n, sizeof(double));
for (i = 0; i <
n; i++) // read array
scanf("%lf", &m[i]);
s = 0; // find the sum
of array elements
for (i = 0; i <
n; i++)
s = s + m[i];
free(m);
printf("%.1lf\n", s); // print the
answer
return 0;
}
Algorithm implementation – dynamic memory allocation, new
#include <stdio.h>
int i, n;
double s;
double *m;
int main(void)
{
scanf("%d",&n);
// allocate memory
for array
m = new double[n];
// read array
for(i = 0; i
< n; i++)
scanf("%lf",m+i);
// m + i = &m[i]
// find the sum of
array elements
s = 0;
for(i = 0; i
< n; i++)
s = s + m[i];
// print the answer
printf("%.1lf\n",s);
// deallocate the
array memory
delete[] m;
return 0;
}
Algorithm implementation – files
When working with files in the e-olymp system, the
following are used:
·
input.txt – input file;
·
output.txt – output
file;
When submitting solutions with files, select the option
“Solution uses files for reading and writing”.
#include <stdio.h>
int i, n;
double s, m[100];
int main(void)
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
scanf("%d",&n);
// read array
for(i = 0; i
< n; i++)
scanf("%lf",&m[i]);
// find the sum of
array elements
s = 0;
for(i = 0; i
< n; i++)
s = s + m[i];
// print the answer
printf("%.1lf\n",s);
return 0;
}
Java implementation
public class
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
con.useLocale(Locale.US);
int n = con.nextInt();
double sum = 0;
for (int i = 0; i < n; i++)
{
double val = con.nextDouble();
sum += val;
}
System.out.printf("%.1f",sum);
con.close();
}
}
Java implementation – own class
import java.util.*;
class
Array
{
private double[] m;
private int size;
public Array(int n)
{
size = n;
m = new double[n];
}
public void
set(int i, double value)
{
m[i] = value;
}
public double getSum()
{
double s = 0;
for(int i = 0; i < size; i++)
s = s + m[i];
return s;
}
}
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
con.useLocale(Locale.US);
int n = con.nextInt();
Array a = new Array(n);
for (int i = 0; i < n; i++)
a.set(i,con.nextDouble());
System.out.printf("%.1f",a.getSum());
con.close();
}
}
Python implementation
Read the input data.
n = int(input())
m = list(map(float,input().split()))
Compute and print the answer.
print(sum(m))